import { User } from '@/models/User'
import { ActionContext } from 'vuex';
import db from '@/components/db'
import _ from 'lodash';

interface State {
    users: User[]
}

const initState = {
    users: []
}

const getters = {
    users(state: State) {
        return state.users
    }
}

const actions = {
    initUsers(context: ActionContext<State, any>) {
        return new Promise((resolve, reject) => {
            db.filter('user').then((users) => {
                let arr: User[] = [];
                if (users instanceof Array) {
                    if (users.length > 0) {
                        users.forEach((item) => {
                            const u = new User(item);
                            arr.push(u);
                        })
                    }
                }
                context.commit('initUsers', arr);
                resolve();
            }).catch(reason => {
                reject(reason);
            })
        })
    },
    addUser(context: ActionContext<State, any>, user: User) {
        return new Promise((resovle, reject) => {
            if (user) {
                if (user.birthday) {
                    user.birthday = user.birthday.valueOf();
                }
                if (user.deathday) {
                    user.deathday = user.deathday.valueOf();
                }
            }
            const u: User = new User(user);
            db.insert('user', u).then(() => {
                context.commit('addUser', u);
                resovle(u)
            }).catch(reason => {
                reject(reason);
            })
        })
    },
    updateUser(context: ActionContext<State, any>, user: User) {
        return new Promise((resolve, reject) => {
            if (user) {
                if (user.birthday) {
                    user.birthday = user.birthday.valueOf();
                }
                if (user.deathday) {
                    user.deathday = user.deathday.valueOf();
                }
            }
            user.update();
            db.update('user', user).then(() => {
                context.commit('updateUser', user);
                resolve();
            }).catch(reason => {
                reject(reason);
            })
        })
    },
    deleteUser(context: ActionContext<State, any>, user: User) {
        return new Promise((resolve, reject) => {
            db.delete('user', { id: user.id, }).then((result) => {
                context.commit('deleteUser', user);
                resolve();
            }).catch(reason => {
                reject(reason);
            })
        })
    },
    getUserById(context: ActionContext<State, any>, id: string) {
        return new Promise((resolve, reject) => {
            db.filter('user', { id }).then((result) => {
                if (result instanceof Array) {
                    if (result.length > 0) {
                        let u = new User(result[0]);
                        resolve(u)
                    } else {
                        reject(new Error('没有找到用户'))
                    }
                } else {
                    reject(new Error('数据库错误'))
                }
            }).catch(reason => {
                reject(reason);
            })
        })
    }
}

const mutations = {
    initUsers(state: State, users: User[]) {
        state.users = users;
        console.log("用户初始化：", state.users);
    },
    addUser(state: State, user: User) {
        state.users.push(user);
    },
    updateUser(state: State, user: User) {
        let index = _.findIndex(state.users, { id: user.id });
        if (index >= 0) {
            state.users.splice(index, 1, user);
        }
    },
    deleteUser(state: State, user: User) {
        let index = _.findIndex(state.users, { id: user.id });
        if (index >= 0) {
            state.users.splice(index, 1);
        }
    }
}

export default {
    state: initState,
    getters,
    actions,
    mutations
}
